package 二叉树相关问题.BST;

import java.util.LinkedList;
import java.util.List;

public class 合法二叉搜索树 {
    List<Integer> list = new LinkedList<>();
    public boolean isValidBST(TreeNode root) {
        if (root==null)
            return true;
       traverse(root);
       int sum=1;
        for (int i = 1; i < list.size(); i++) {
            if (list.get(i)>list.get(i-1))
                sum++;
        }
        return sum==list.size();
    }

    private void traverse(TreeNode root) {
        if (root==null)
            return;
        traverse(root.left);
        list.add(root.val);
        traverse(root.right);
    }
}
